require 'rubygems'
require 'mechanize'
require 'xml'

STDOUT.sync = true

codelist = ['ACCT', 'BIEN','BTEC','CBME','CENG','CHEM','CIEM','CIVL','COMP','CSIT',
  'ECON','EEMT','EESM','ELEC','ENEG','ENGG','ENVR','ENVS','EVNG','EVSM',
  'FINA','FYTG','GBUS','GNED','HART','HLTH','HUMA','IBTM','IELM','ISOM',
  'JEVE','LABU','LAGR','LANG','LIFS','MAFS','MALS','MARK','MATH','MECH',
  'MESF','MGMT','NANO','PDEV','PHYS','RMBI','SBMT','SCED','SCIE','SHSS','SOSC','SSMA','TEMG','UROP']

def TheNode(name, content)
  n = XML::Node.new(name)
  n << content
  return n
end

def tablesToNodes(tables)
  tables = tables[1..-1]
  output = []
  tables.each do |c|
    
    tds = c.search('td')
    
    if(tds.length<4)
      puts c.to_s
      next
    end
    
    code = /\w\w\w\w\s\d+\w*/.match(tds[0].text)
    if(code == nil)
      puts tds[0].text+" code invalid"
      next
    end
    
    puts code
    
    clNodeChild = XML::Node.new("course")
    clNodeChild['code'] = code.to_s
    
    name = tds[1].text
    unit = /\d/.match(tds[2].text)
    if(unit == nil)
      unit = 0
    else
      unit = unit[0]
    end
    
    desc = tds[3].text.strip
    clNodeChild << TheNode('name',name)
    clNodeChild << TheNode('unit',unit)
    clNodeChild << TheNode('desc',desc)   
    output.push(clNodeChild) 
  end
  return output
end

agent = Mechanize.new
count = 0
codelist.each do |cl|
  
  sleep(3)
  page = agent.get('http://publish.ust.hk/SISCourseCat/ShowUGCourseList.aspx?Subject='+cl+'&WebSite=Production')
  ugcourses = page.search('table')
  
  page2 = agent.get('http://publish.ust.hk/SISCourseCat/ShowPGCourseList.aspx?Subject='+cl+'&WebSite=Production')
  pgcourses = page2.search('table')
  
  if(ugcourses.length==0)
    c1 = []
  else
    c1 = tablesToNodes(ugcourses)
  end
  if(pgcourses.length==0)
    c2 = []
  else
    c2 = tablesToNodes(pgcourses)
  end 
  courses = c1 + c2
  
  f = XML::Document.new
  f.root = XML::Node.new(cl)
  courses.each do |eachc|
    f.root << eachc
  end 
  
  puts cl + " count: "+count.to_s
  f.save(cl+'.xml',:indent=>true, :encoding => XML::Encoding::UTF_8 )
  count = count + 1
end
  